package com.xinzhidi.web.equipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinzhidi.common.core.domain.model.LoginUser;
import com.xinzhidi.common.core.page.TableDataInfo;
import com.xinzhidi.common.core.text.Convert;
import com.xinzhidi.common.utils.DateUtils;
import com.xinzhidi.common.utils.SecurityUtils;
import com.xinzhidi.common.utils.StringUtils;
import com.xinzhidi.common.utils.bean.BeanUtils;
import com.xinzhidi.system.service.ISysUserService;
import com.xinzhidi.web.equipment.domain.EquipmentGroup;
import com.xinzhidi.web.equipment.domain.dto.EquipmentGroupAddDto;
import com.xinzhidi.web.equipment.domain.dto.EquipmentGroupListDto;
import com.xinzhidi.web.equipment.domain.dto.EquipmentGroupUpdDto;
import com.xinzhidi.web.equipment.domain.vo.EquipmentGroupInfoVo;
import com.xinzhidi.web.equipment.domain.vo.EquipmentGroupListVo;
import com.xinzhidi.web.equipment.mapper.EquipmentGroupMapper;
import com.xinzhidi.web.equipment.service.IEquipmentGroupService;
import com.xinzhidi.web.plot.domain.entity.CPlot;
import com.xinzhidi.web.plot.domain.vo.SelectVo;
import com.xinzhidi.web.plot.service.ICPlotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 设备分组Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-26
 */
@Service
public class EquipmentGroupServiceImpl extends ServiceImpl<EquipmentGroupMapper, EquipmentGroup> implements IEquipmentGroupService {
    @Autowired
    private EquipmentGroupMapper equipmentGroupMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ICPlotService plotService;
    /**
     * 查询设备分组
     *
     * @param id 设备分组主键
     * @return 设备分组
     */
    @Override
    public EquipmentGroupInfoVo selectEquipmentGroupById(Long id) {
        EquipmentGroup equipmentGroup = equipmentGroupMapper.selectById(id);
        EquipmentGroupInfoVo infoVo = new EquipmentGroupInfoVo();
        BeanUtils.copyProperties(equipmentGroup, infoVo);
        return infoVo;
    }

    /**
     * 查询设备分组列表
     *
     * @param dto 查询设备分组dto
     * @return 设备分组
     */
    @Override
    public TableDataInfo<EquipmentGroupListVo> selectEquipmentGroupList(EquipmentGroupListDto dto) {
        LambdaQueryWrapper<EquipmentGroup> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(dto.getPlotId() != null, EquipmentGroup::getPlotId, dto.getPlotId());
        wrapper.like(StringUtils.isNotEmpty(dto.getGroupName()), EquipmentGroup::getGroupName, dto.getGroupName());
        if (CollectionUtils.isNotEmpty(dto.getCreateTime()) && dto.getCreateTime().size() > 1 ){
            wrapper.ge(EquipmentGroup::getCreateTime,dto.getCreateTime().get(0)+" 00:00:00");
            wrapper.le(EquipmentGroup::getCreateTime,dto.getCreateTime().get(1)+" 23:59:59");
        }
        wrapper.orderByDesc(EquipmentGroup::getId);
        List<EquipmentGroup> equipmentGroups = equipmentGroupMapper.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(equipmentGroups)){
            Map<Long, String> idMapName = plotService.list(new LambdaQueryWrapper<>(CPlot.class).in(CPlot::getId, equipmentGroups.stream().map(EquipmentGroup::getPlotId).collect(Collectors.toList())))
                    .stream().collect(Collectors.toMap(CPlot::getId, CPlot::getParkName));
            return TableDataInfo.getDataTable(equipmentGroupMapper.selectList(wrapper), (domain -> {
                EquipmentGroupListVo vo = new EquipmentGroupListVo();
                BeanUtils.copyProperties(domain, vo);
                vo.setCreateName(sysUserService.selectUserById(Convert.toLong(domain.getCreateBy())).getNickName());
                vo.setPlotName(idMapName.get(Convert.toLong(vo.getPlotId())));
                return vo;
            }));
        }else{
            return TableDataInfo.getDataTable(equipmentGroups,( domain ->{return null;}));
        }
    }

    /**
     * 查询设备分组下拉列表
     *
     * @param dto 查询设备分组dto
     * @return 设备分组
     */
    @Override
    public TableDataInfo<SelectVo> listEquipmentGroupSelect(EquipmentGroupListDto dto){
        LambdaQueryWrapper<EquipmentGroup> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(dto.getPlotId() != null, EquipmentGroup::getPlotId, dto.getPlotId());
        wrapper.like(StringUtils.isNotEmpty(dto.getGroupName()), EquipmentGroup::getGroupName, dto.getGroupName());
        if (CollectionUtils.isNotEmpty(dto.getCreateTime()) && dto.getCreateTime().size() > 1 ){
            wrapper.ge(EquipmentGroup::getCreateTime,dto.getCreateTime().get(0)+" 00:00:00");
            wrapper.le(EquipmentGroup::getCreateTime,dto.getCreateTime().get(1)+" 23:59:59");
        }
        wrapper.orderByDesc(EquipmentGroup::getId);
        List<EquipmentGroup> equipmentGroups = equipmentGroupMapper.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(equipmentGroups)){
            Map<Long, String> idMapName = plotService.list(new LambdaQueryWrapper<>(CPlot.class).in(CPlot::getId, equipmentGroups.stream().map(EquipmentGroup::getPlotId).collect(Collectors.toList())))
                    .stream().collect(Collectors.toMap(CPlot::getId, CPlot::getParkName));
            return TableDataInfo.getDataTable(equipmentGroupMapper.selectList(wrapper), (domain -> {
                SelectVo vo = new SelectVo();
                vo.setLabel(domain.getGroupName());
                vo.setValue(domain.getId().toString());
                return vo;
            }));
        }else{
            return TableDataInfo.getDataTable(equipmentGroups,( domain ->{return null;}));
        }
    }

    /**
     * 新增设备分组
     *
     * @param dto 新增设备分组dto
     * @return 结果
     */
    @Override
    public int insertEquipmentGroup(EquipmentGroupAddDto dto) {

        checkExist(null, dto.getPlotId(), dto.getGroupName());
        LoginUser loginUser = SecurityUtils.getLoginUser();

        EquipmentGroup equipmentGroup = new EquipmentGroup();
        BeanUtils.copyProperties(dto, equipmentGroup);
        equipmentGroup.setCreateBy(loginUser.getUserId().toString());
        equipmentGroup.setCreateTime(DateUtils.getNowDate());
        return equipmentGroupMapper.insert(equipmentGroup);
    }

    /**
     * 修改设备分组
     *
     * @param dto 修改设备分组dto
     * @return 结果
     */
    @Override
    public int updateEquipmentGroup(EquipmentGroupUpdDto dto) {
        checkExist(dto.getId(), dto.getPlotId(), dto.getGroupName());
        LoginUser loginUser = SecurityUtils.getLoginUser();

        EquipmentGroup equipmentGroup = new EquipmentGroup();
        BeanUtils.copyProperties(dto, equipmentGroup);
        equipmentGroup.setUpdateBy(loginUser.getUserId().toString());
        equipmentGroup.setUpdateTime(DateUtils.getNowDate());
        return equipmentGroupMapper.updateById(equipmentGroup);
    }

    /**
     * 判断设备分组名称是否已存在
     *
     * @param id        分组id
     * @param lotId     小区id
     * @param groupName 分组名称
     * @return 结果
     */
    private void checkExist(Long id, String lotId, String groupName) {
        LambdaQueryWrapper<EquipmentGroup> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EquipmentGroup::getPlotId, lotId);
        wrapper.eq(EquipmentGroup::getGroupName, groupName);
        wrapper.ne(id != null, EquipmentGroup::getId, id);
        int count = equipmentGroupMapper.selectCount(wrapper);
        if (count > 0) {
            throw new RuntimeException("该小区分组名称已存在！");
        }
    }

    /**
     * 批量删除设备分组
     *
     * @param ids 需要删除的设备分组主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentGroupByIds(Long[] ids) {
        return equipmentGroupMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除设备分组信息
     *
     * @param id 设备分组主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentGroupById(Long id) {
        return equipmentGroupMapper.deleteById(id);
    }
}
